package com.yohann.boot.config.aspect;

import com.yohann.boot.common.exception.ExceptionEnum;
import com.yohann.boot.common.exception.GlobalException;
import com.yohann.boot.common.response.Result;
import com.yohann.boot.util.LogUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;

import java.util.stream.Collectors;

import static com.yohann.boot.common.exception.ExceptionEnum.NULL_POINTER;
import static com.yohann.boot.common.exception.ExceptionEnum.SERVER_ERROR;

/**
 * <p>
 * 全局异常处理
 * </p>
 *
 * @author Yohann
 * @since 2020/9/21
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 空指针异常拦截
     *
     * @param e 异常信息
     * @return ResponseBody
     */
    @ExceptionHandler(NullPointerException.class)
    public Result<String> error(Exception e) {
        StackTraceElement traceElement = e.getStackTrace()[0];
        String errorMessage = LogUtils.getStackTraceStr(traceElement);
        LogUtils.error(errorMessage, e);
        return Result.error().code(NULL_POINTER.getCode()).message(NULL_POINTER.getMessage() + ":" + errorMessage);
    }

    /**
     * 自定义异常拦截
     *
     * @param e 异常信息
     * @return ResponseBody
     */
    @ExceptionHandler(GlobalException.class)
    public Result<String> error(GlobalException e) {
        LogUtils.error(e);
        return Result.error().code(e.getCode()).message(e.getMessage());
    }

    /**
     * http客户端异常
     *
     * @param e 异常信息
     * @return ResponseBody
     */
    @ExceptionHandler(HttpClientErrorException.class)
    public Result<String> error(HttpClientErrorException e) {
        LogUtils.error(e);
        return Result.error().code(SERVER_ERROR.getCode()).message(e.getResponseBodyAsString());
    }

    /**
     * 自定义异常拦截
     *
     * @param throwable 异常信息
     * @return ResponseBody
     */
    @ExceptionHandler(Throwable.class)
    public Result<String> error(Throwable throwable) {
        LogUtils.error(throwable);
        return Result.error().code(SERVER_ERROR.getCode()).message(SERVER_ERROR.getMessage() + ":" + throwable.getMessage());
    }

    /**
     * 参数绑定异常拦截
     *
     * @param e 异常信息
     * @return ResponseBody
     */
    @ExceptionHandler(BindException.class)
    public Result<String> error(BindException e) {
        String errorMessage = e.getFieldErrors().stream()
                .map(fieldError -> fieldError.getField() + ":" + fieldError.getDefaultMessage())
                .collect(Collectors.joining(","));
        LogUtils.error(errorMessage, e);
        return Result.error().code(ExceptionEnum.PARAMETER_ERROR.getCode()).message(errorMessage);
    }

    /**
     * 方法参数无效异常异常拦截
     *
     * @param e 异常信息
     * @return ResponseBody
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> error(MethodArgumentNotValidException e) {
        String errorMessage = e.getBindingResult().getFieldErrors().stream()
                .map(fieldError -> fieldError.getField() + ":" + fieldError.getDefaultMessage())
                .collect(Collectors.joining(","));
        LogUtils.error(errorMessage, e);
        return Result.error().code(ExceptionEnum.PARAMETER_ERROR.getCode()).message(errorMessage);
    }

}